<template>
	<view>
		
		<uni-forms style="margin-left: 2.5%; margin-right: 2.5%; margin-top: 5%;" ref="form" v-model="activity">			
			<uni-list v-for="(stage,index) in stages" >
				<uni-list-item>
				<template v-slot:body>
					<uni-forms>
					<uni-section style="margin-bottom: 5%;" :title="stage" type="line">
					</uni-section>
					<uni-forms-item label="开始时间" required>
						<uni-datetime-picker type="datetime" returnType="string" v-model="begintimes[index]"  />
					</uni-forms-item>
					<uni-forms-item label="结束时间">
						<uni-datetime-picker type="datetime" returnType="string" v-model="endtimes[index]"/>
					</uni-forms-item>
					<uni-forms-item label="地点" required>
						<uni-easyinput v-model="places[index]"/>
					</uni-forms-item>
					</uni-forms>
				</template>
			</uni-list-item>
			</uni-list>	
			<uni-forms-item label="上传图片">
				<button @click="choosepic()">上传</button>
			</uni-forms-item>
			<uni-forms-item label="简介" required>
				<uni-easyinput type = "textarea" maxlength=-1 autoHeight  v-model="activity.info"/>
			</uni-forms-item>
			<uni-forms-item label="活动内容" required="">
				<uni-easyinput type = "textarea"  autoHeight maxlength=-1 v-model="activity.content"/>
			</uni-forms-item>
		</uni-forms>
		<view>
			<button type="primary" @click="submit">提交</button>
		</view>
		<view>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :type="msgType" cancelText="关闭" @confirm="backToHome" confirmText="确认" title="提示信息" :content="messageText"></uni-popup-dialog>
		</uni-popup>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				activity:{hostUid:2,
				name:"", 
				beginTime:"", 
				endTime:"", 
				place:"", 
				type:"", 
				state:"checking", 
				info:"", 
				addedTimes:0,
				stageNames:"",
				stageCount:0,
				content:"",
				pic1:'',
				pic2:'',
				pic3:'',
				},
				text:["开始时间","结束时间","地点"],
				stages:[],
				pics:[],
				begintimes:[],
				endtimes:[],
				places:[],
				msgType:"success",
				messageText:"",
				token:""
			}
		},
		onLoad(){
			this.activity = uni.getStorageSync("activity");
			this.token = uni.getStorageSync("token")
			this.stages =  this.activity.stageNames.split("，");
			this.activity.stageCount = this.activity.stageNames.split("，").length;
			console.log(this.activity.stageCount)
		},
		methods: {
			backToHome(){
				if(this.msgType == "success")
				uni.navigateBack({
					delta:2
				})
			},
			messageToggle(msg) {
				this.messageText = msg
				this.$refs.alertDialog.open()
			},
			choosepic(){
				uni.chooseImage({
					count: 3, 
					sourceType: ['album'], 
					success:(res)=> {
						this.pics = res.tempFilePaths;
					}
				})
			},
			show(){
				for(let i =0;i<this.activity.stageCount;i++)
					console.log(this.begintimes[i]==undefined);
			}, 
			submit(){
				let verify = true;
				let msg = "";
				this.msgType = "success"
				
				if(verify){
					for(let i =0;i<this.activity.stageCount;i++)
					{
						if(this.begintimes[i] == undefined||this.begintimes[i].length<19){
							verify = false;
							msg = this.stages[i]+ " 阶段开始日期和时间不能为空";
							break;
						}
						
						if(this.endtimes[i] != undefined&&this.endtimes[i]!=''){
							if(this.endtimes[i].length<19){
								verify = false;
								msg = this.stages[i]+" 如果填写阶段结束日期，则阶段结束时间不能为空";
								break;
							}
							
							if(this.endtimes[i]<=this.begintimes[i]){
								verify = false;
								msg = this.stages[i]+ " 阶段结束日期时间要晚于阶段开始日期时间";
								break;
							}
						}
					}
				}
				
				let begintimes = "";
				let endtimes = "";
				let temptime = "";
				for(let i = 0;i<this.begintimes.length;i++){
					if(i!=this.begintimes.length-1)
					begintimes+=this.begintimes[i]+"，";
					else
					begintimes+=this.begintimes[i];
				}
				
				for(let i =0;i<this.activity.stageCount;i++){
					if(this.endtimes[i]==undefined||this.endtimes[i]==''){
						temptime="时间待定";
					}
					else{
						temptime = this.endtimes[i];
					}
					console.log(temptime);
					
					if(i!=this.activity.stageCount-1){
						endtimes+=temptime+"，";
					}
					else{
						endtimes+=temptime;	
					}
					console.log(endtimes)
				}
				if(verify){
					for(let i =0;i<this.activity.stageCount;i++){
					if(this.places[i] == undefined){
						verify = false;
						msg =this.stages[i] + " 阶段地点不能为空";
					}
					}
				}
				let places = "";
				for(let i =0;i<this.places.length;i++)
					if(i!=this.places.length-1){
						places+=this.places[i]+"，";
					}
					else{
						places+=this.places[i];
					}
				
				
				if(verify){
					if(this.activity.info == ""){
						verify = false;
						msg = "活动简介不能为空"
					}
				}
				if(verify){
					if(this.activity.content == ""){
						verify = false;
						msg = "活动内容不能为空";
					}
				}
				if(verify){				
				//整合信息
				this.activity.beginTime = begintimes;
				this.activity.endTime = endtimes;
				this.activity.place = places;
				//先发送activiy
				uni.request({
					url:"http://127.0.0.1:8080/activity",
					data:this.activity,
					method:"POST",
					formData:{
						Authorization : "Bearer " +this.token,
					},
					header:{
						Authorization : "Bearer " +this.token,
						
					},
					success: (res) => {
						if(res.statusCode==200){
						this.activityId = res.data.id
						let size = this.pics.length;
						let i = 0;
						if(i<size)
						uni.uploadFile({
							url: 'http://127.0.0.1:8080/activity/setpic', 
							filePath: this.pics[0],
							name: 'pic',
							header:{
								Authorization : "Bearer " +this.token,
								
								//uni.getStorageSync("token")
							},
							formData:{
								'id': this.activityId,
								'count':i+1
							},
							success: (uploadFileRes) => {
								i++
								if(i<size)
								uni.uploadFile({
									url: 'http://127.0.0.1:8080/activity/setpic', 
									filePath: this.pics[1],
									name: 'pic',
									header:{
										Authorization : "Bearer " +this.token,
										
										//uni.getStorageSync("token")
									},
									formData:{
										'id': this.activityId,
										'count':i+1
									},
									success: (uploadFileRes) => {
										i++
										if(i<size)
										uni.uploadFile({
											url: 'http://127.0.0.1:8080/activity/setpic', 
											filePath: this.pics[2],
											name: 'pic',
											header:{
												Authorization : "Bearer " +this.token,
												
												//uni.getStorageSync("token")
											},
											formData:{
												'id': this.activityId,
												'count':i+1
											},
											success: (uploadFileRes) => {
												this.messageToggle("提交成功")
											}
										});
										else{
											this.messageToggle("提交成功")
										}
								}
							});
							else{
								this.messageToggle("提交成功")

							}
							}
							
						});
						else{
							uni.uploadFile({
								url: 'http://127.0.0.1:8080/activity/setpic',
								filePath: "../../static/默认活动.png",
								name: 'pic',
								header:{
									Authorization : "Bearer " +this.token,
								},
								formData:{
									'id': this.activityId,
									'count':i+1
								},
								success: () => {
									this.messageToggle("提交成功")
								}
							})
						}
					}
						else{
							this.messageToggle(msg);
						}
					}
				})
				}
				else{
					this.msgType = "error";
					this.messageToggle(msg);
				}
			},
		}
	}
</script>

<style>

</style>
